//
//  JJTemperatuTreDetectionCell.m
//  Estate
//
//  Created by houzhen on 2017/11/11.
//  Copyright © 2017年 ttsx. All rights reserved.
//

#import "JJTemperatuTreDetectionCell.h"

@implementation JJTemperatuTreDetectionCell
- (void)setIsEdit:(BOOL)isEdit{
    if(isEdit){
        self.cyField.delegate=self;
        self.dcField.delegate=self;
        self.tpField.delegate=self;
        self.zyField.delegate=self;
        self.addBtn.hidden = NO;
    
    }else{
        self.cyField.userInteractionEnabled=NO;
        self.dcField.userInteractionEnabled=NO;
        self.tpField.userInteractionEnabled=NO;
        self.zyField.userInteractionEnabled=NO;
        self.addBtn.hidden = YES;

    }
}
- (void)awakeFromNib {
    [super awakeFromNib];
    self.selectionStyle=UITableViewCellSelectionStyleNone;

    self.backView.backgroundColor = [UIColor colorWithRed:0.94 green:0.94 blue:0.94 alpha:1.00];
    self.backView.layer.cornerRadius = 5;
    self.backView.layer.masksToBounds = YES;
    
    [self.addBtn setBackgroundColor:[TTTheme defaultManger].defaultColor];
    self.addBtn.layer.cornerRadius = self.addBtn.frame.size.height/2;
    self.addBtn.layer.masksToBounds = YES;
    self.cyField.keyboardType=UIKeyboardTypeNumberPad;
    self.dcField.keyboardType=UIKeyboardTypeNumberPad;
    self.tpField.keyboardType=UIKeyboardTypeNumberPad;
    self.zyField.keyboardType=UIKeyboardTypeNumberPad;
    self.titleLabel.font = [UIFont systemFontOfSize:16*BIZ];
    self.cyLabel.font = [UIFont systemFontOfSize:16*BIZ];
    self.dcLabel.font = [UIFont systemFontOfSize:16*BIZ];
    self.tpLabel.font = [UIFont systemFontOfSize:16*BIZ];
    self.zyLabel.font = [UIFont systemFontOfSize:16*BIZ];

    self.titleLabel.textColor = UIColorFromRGB(0x797979);
    self.cyLabel.textColor = UIColorFromRGB(0x797979);
    self.dcLabel.textColor = UIColorFromRGB(0x797979);
    self.tpLabel.textColor = UIColorFromRGB(0x797979);
    self.zyLabel.textColor = UIColorFromRGB(0x797979);

    // Initialization code
}
- (IBAction)dcFieldChange:(ESTextField *)sender {
    if(self.dcFieldChangeBlock){
        self.dcFieldChangeBlock(self.dcField.text);
    }
}
- (IBAction)cyFieldChange:(ESTextField *)sender {
    if(self.cyFieldChangeBlock){
        self.cyFieldChangeBlock(self.cyField.text);
    }
}
- (IBAction)tpFieldChange:(ESTextField *)sender {
    if(self.tpFieldChangeBlock){
        self.tpFieldChangeBlock(self.tpField.text);
    }
}
- (IBAction)zyFieldChange:(ESTextField *)sender {
    if(self.zyFieldChangeBlock){
        self.zyFieldChangeBlock(self.zyField.text);
    }
}
#pragma mark UITextFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.zyField resignFirstResponder];
    [self.cyField resignFirstResponder];
    [self.tpField resignFirstResponder];
    [self.dcField resignFirstResponder];

    return  YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    //   限制苹果系统输入法  禁止输入表情
    if ([[[UITextInputMode currentInputMode]primaryLanguage] isEqualToString:@"emoji"]) {
        return NO;
    }
    BOOL isHaveDian = YES;
    if ([textField.text rangeOfString:@"."].location == NSNotFound) {
        isHaveDian = NO;
    }
    
    //输入的字符是否是小数点
    if ([string length] > 0) {
        unichar single = [string characterAtIndex:0];//当前输入的字符
        if ((single >= '0' && single <= '9') || single == '.') {//数据格式正确
            //判断长度
            if(!isHaveDian && [textField.text length] >= 10){
                [textField.text stringByReplacingCharactersInRange:range withString:@""];
                return NO;
            }
            //首字母不能为0和小数点
            if([textField.text length] == 0){
                if(single == '.') {
                    [self showError:@"第一个数字不能为小数点"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
                if (single == '0') {
                    [self showError:@"第一个数字不能为0"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }
            //输入的字符是否是小数点
            if (single == '.') {
                if(!isHaveDian)//text中还没有小数点
                {
                    isHaveDian = YES;
                    return YES;
                }else{
                    [self showError:@"您已经输入过小数点了"];
                    [textField.text stringByReplacingCharactersInRange:range withString:@""];
                    return NO;
                }
            }else{
                if (isHaveDian) {//存在小数点
                    //判断小数点的位数
                    NSRange ran = [textField.text rangeOfString:@"."];
                    if (range.location - ran.location <= 2) {
                        return YES;
                    }else{
                        [self showError:@"最多输入两位小数"];
                        return NO;
                    }
                }else{
                    return YES;
                }
            }
        }else{//输入的数据格式不正确
            [self showError:@"输入的格式不正确"];
            [textField.text stringByReplacingCharactersInRange:range withString:@""];
            return NO;
        }
    }
    return YES;
}

-(void)showError:(NSString *)msg{
    
    [LSUtility showToastWithMessage:msg inView:[UIApplication sharedApplication].keyWindow Andtime:1];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

@end
